iT邦幫忙

2021 iThome 鐵人賽

DAY 23
0
Mobile Development

就是從無到有寫app系列 第 23

第23天~又是JSON+ListView

  • 分享至 

  • xImage
  •  

又是JSON

開新專案
https://ithelp.ithome.com.tw/upload/images/20220204/20119035FZxsenvBNG.png

準備XML檔+ListView
https://ithelp.ithome.com.tw/upload/images/20220204/20119035zo1o4jk4Ym.png

放好ID


準備一個TXT檔案-

https://ithelp.ithome.com.tw/upload/images/20220204/20119035c2xXc7LHgQ.png


把txt檔案貼到java檔裡

先宣告變數-

https://ithelp.ithome.com.tw/upload/images/20220204/20119035ZD9lYmJ0bK.png

TXT檔案貼上會自動解析成字串格式-

{
    title:JSONParserTutorial,
    array:[
    {
       company:Google
    },
    {
       company:Facebook
    },
    {
       company:LinkedIn
    },
    {
       company:Microsoft
    },
    {
       company:Apple
    },
    ],

       nested:{
       flag:ture,
       random_number:1

    }
}

https://ithelp.ithome.com.tw/upload/images/20220204/20119035mpbZUTHT03.png

綠色都是要得~

宣告按鈕跟容器-

https://ithelp.ithome.com.tw/upload/images/20220204/20119035Z5NrpRrwAW.png

package com.huang.myjson2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //宣告變數
    String json_string = "{\n" +
            "    title:JSONParserTutorial,\n" +
            "    array:[\n" +
            "    {\n" +
            "       company:Google\n" +
            "    },\n" +
            "    {\n" +
            "       company:Facebook\n" +
            "    },\n" +
            "    {\n" +
            "       company:LinkedIn\n" +
            "    },\n" +
            "    {\n" +
            "       company:Microsoft\n" +
            "    },\n" +
            "    {\n" +
            "       company:Apple\n" +
            "    },\n" +
            "    ],\n" +
            "\n" +
            "       nested:{\n" +
            "       flag:ture,\n" +
            "       random_number:1\n" +
            "\n" +
            "    }\n" +
            "}";

    ListView listView;
    ArrayList<String> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化
        
    }
}

再來是初始化-

反紅是因為要拋例外-點到紅色線

https://ithelp.ithome.com.tw/upload/images/20220204/20119035RQuHQsXqBV.png

長這樣
https://ithelp.ithome.com.tw/upload/images/20220204/20119035RS3EyVw1vY.png


package com.huang.myjson2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //宣告變數
    String json_string = "{\n" +
            "    title:JSONParserTutorial,\n" +
            "    array:[\n" +
            "    {\n" +
            "       company:Google\n" +
            "    },\n" +
            "    {\n" +
            "       company:Facebook\n" +
            "    },\n" +
            "    {\n" +
            "       company:LinkedIn\n" +
            "    },\n" +
            "    {\n" +
            "       company:Microsoft\n" +
            "    },\n" +
            "    {\n" +
            "       company:Apple\n" +
            "    },\n" +
            "    ],\n" +
            "\n" +
            "       nested:{\n" +
            "       flag:ture,\n" +
            "       random_number:1\n" +
            "\n" +
            "    }\n" +
            "}";

    ListView listView;
    ArrayList<String> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化
        listView = findViewById(R.id.list_view);
        items = new ArrayList<>();

        try {
            JSONObject object = new JSONObject(json_string);
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}


如何把它轉到listView
https://ithelp.ithome.com.tw/upload/images/20220204/20119035oEq8N3ata2.png

package com.huang.myjson2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //宣告變數
    String json_string = "{\n" +
            "    title:JSONParserTutorial,\n" +
            "    array:[\n" +
            "    {\n" +
            "       company:Google\n" +
            "    },\n" +
            "    {\n" +
            "       company:Facebook\n" +
            "    },\n" +
            "    {\n" +
            "       company:LinkedIn\n" +
            "    },\n" +
            "    {\n" +
            "       company:Microsoft\n" +
            "    },\n" +
            "    {\n" +
            "       company:Apple\n" +
            "    },\n" +
            "    ],\n" +
            "\n" +
            "       nested:{\n" +
            "       flag:ture,\n" +
            "       random_number:1\n" +
            "\n" +
            "    }\n" +
            "}";

    ListView listView;
    ArrayList<String> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化
        listView = findViewById(R.id.list_view);
        items = new ArrayList<>();

        try {
            JSONObject object = new JSONObject(json_string);
            JSONArray array = object.getJSONArray("array");
            for(int i;i<array.length();i++){
                JSONObject o=array.getJSONObject(i);
                items.add(o.getString("company"));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        //把資料按照格式轉好

    }
}

//把資料按照格式轉好

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //宣告變數
    String json_string = "{       \n" +
            "\ttitle:JSONParserTutorial,       \n" +
            "\tarray:[       \n" +
            "  {       \n" +
            "\tcompany:Google           \n" +
            "  },       \n" +
            "  {       \n" +
            "\tcompany:Facebook           \n" +
            "  },       \n" +
            "  {       \n" +
            "\tcompany:LinkedIn           \n" +
            "  },       \n" +
            "  {       \n" +
            "\tcompany:Microsoft           \n" +
            "  },       \n" +
            "  {       \n" +
            "\tcompany:Apple           \n" +
            "  }       \n" +
            "  ],       \n" +
            "\tnested:{       \n" +
            "\tflag:true,       \n" +
            "\trandom_number:1       \n" +
            "  }       \n" +
            "}  ";
    ListView listView;
    ArrayList<String> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.list_view);
        items = new ArrayList<String>();

        try {
            JSONObject object = new JSONObject(json_string);
            JSONArray array = object.getJSONArray("array");
            for(int i=0; i<array.length(); i++){
                JSONObject o = array.getJSONObject(i);
                items.add(o.getString("company"));
            }
            //取得其他key的內容-----------------------
            JSONObject oo = object.getJSONObject("nested");
            String msg = oo.getString("flag");
            boolean msg2 = oo.getBoolean("flag");
            Toast.makeText(MainActivity.this, msg2+"", Toast.LENGTH_SHORT).show();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_list_item_1,
                items
        );
        listView.setAdapter(adapter);

    }
}

這裡我的模擬器RUN不出來.
所以我自己上網找另外一個做看看

/images/emoticon/emoticon04.gif

後來發現是要用手機看-
https://ithelp.ithome.com.tw/upload/images/20220204/20119035twZD8dJvCm.jpg


所以我自己上網找另外一個做看看

java 檔

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView=(ListView) findViewById(R.id.listview);

        String[] str ={"新北市","台北市","台中市","台南市","高雄市"};

        ArrayAdapter adapter =new ArrayAdapter(this,
                android.R.layout.simple_list_item_1,
                str);

        listView.setAdapter(adapter);
    }
}

xml檔

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:id="@+id/listview" />

</LinearLayout>

也是用自己的手機測試

https://ithelp.ithome.com.tw/upload/images/20220204/20119035whLc6bkhkl.jpg


我先自己承認~因為下周要面試所以都在準備C#........
不過還是至少要來個純文字版~
其實我覺得我寫的Android除非是從0開始~
不然我覺得我寫的小筆記蠻實用的0.0(老王賣瓜)
尤其是對英文不好的來說~

繼續來寫Android的XML的編輯功能:

  1. margin邊界
  2. 內距padding=空多少空間不放字(按箭頭往下填)
  3. 對齊gravity
  4. 按紐=toggleButton
  5. 按鈕放上去之後=黃色!可以~ 紅色!不行因為代表錯誤
  6. 要確認按鈕4邊至少要有一邊有綁
  7. 按紐=Button (沒有綁ID)
  8. 賦予功能使用的語法:toggleButton.setOnCheckedChangeListener();
  9. button一定會縮排-要有2個選項要選2個button
  10. Compontaint Tree預設是由上往下排

希望...可以讓我繼續有鐵人發文的機會~拜託拜託


上一篇
第22天~JSON / GSON
下一篇
第24天~Firebase
系列文
就是從無到有寫app31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言